package com.karson.common.util;

import org.springframework.http.MediaType;

/**
 * 文件类型的工具类
 */
public class FileTypeUtil {

	/**
	 * 图片类型
	 */
	public final static String[] IMG_FILE_TYPE = {"jpg", "bmp", "png", "gif", "jpeg"};

	/**
	 * 视频类型
	 */
	public final static String[] VIDEO_FILE_TYPE = {"mp4", "gmf", "wmv", "avi"};

	/**
	 * 日志文件
	 */
	public final static String[] LOG_FILE_TYPE = {"log", "txt", "doc", "docx"};

	/**
	 * 音频文件（补全常见格式）
	 */
	public final static String[] AUDIO_FILE_TYPE = {"wav", "mp3", "flac", "aac", "ogg", "m4a", "ape"};

	/**
	 * 字幕文件类型
	 */
	public final static String[] SUBTITLE_FILE_TYPE = {"srt", "ass", "vtt", "lrc"};

	/**
	 * 图片文件
	 */
	public final static int IMG_FILE = 1;
	/**
	 * 视频文件
	 */
	public final static int VIDEO_FILE = 2;

	/**
	 * 日志文件
	 */
	public final static int LOG_FILE = 3;

	/**
	 * 音频文件
	 */
	public final static int AUDIO_FILE = 4;

	/**
	 * 字幕文件
	 */
	public final static int SUBTITLE_FILE = 5;

	/**
	 * 通过文件类型判断文件是否符合所属类型
	 */
	public static String isFileTypeExists(int fileType, String fileName) {
		switch (fileType) {
			case LOG_FILE:
				return bLogFileType(fileName) ? null : "日志类型不正确!";
			case IMG_FILE:
				return bImgFileType(fileName) ? null : "图片类型不正确!";
			case VIDEO_FILE:
				return bVideoFileType(fileName) ? null : "视频类型不正确!";
			case AUDIO_FILE:
				return bAudioFileType(fileName) ? null : "音频类型不正确!";
			case SUBTITLE_FILE:
				return bSubtitleFileType(fileName) ? null : "字幕类型不正确!";
			default:
				return "没有指定文件类型";
		}
	}

	/**
	 * 检查是否日志文件
	 */
	public static boolean bLogFileType(String fileName) {
		String fileSuffix = getFileSuffix(fileName);
		for (String s : LOG_FILE_TYPE) {
			if (s.equalsIgnoreCase(fileSuffix)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 检查是否音频文件
	 */
	public static boolean bAudioFileType(String fileName) {
		String fileSuffix = getFileSuffix(fileName);
		for (String s : AUDIO_FILE_TYPE) {
			if (s.equalsIgnoreCase(fileSuffix)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 检查是否字幕文件
	 */
	public static boolean bSubtitleFileType(String fileName) {
		String fileSuffix = getFileSuffix(fileName);
		for (String s : SUBTITLE_FILE_TYPE) {
			if (s.equalsIgnoreCase(fileSuffix)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 判断文件名是否为图片类型
	 */
	public static boolean bImgFileType(String fileName) {
		String fileSuffix = getFileSuffix(fileName);
		return isImgType(fileSuffix);
	}

	/**
	 * 视频格式
	 */
	public static boolean bVideoFileType(String fileName) {
		String fileSuffix = getFileSuffix(fileName);
		for (String s : VIDEO_FILE_TYPE) {
			if (s.equalsIgnoreCase(fileSuffix)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 获取文件后缀
	 */
	public static String getFileSuffix(String fileName) {
		return fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
	}

	/**
	 * 获取文件名
	 */
	public static String getFileName(String fileName) {
		int dot = fileName.lastIndexOf('.');
		String filename = null;
		if (dot > -1) {
			filename = fileName.substring(0, dot);
		}
		return filename;
	}

	/**
	 * 判断文件名是否为图片类型
	 */
	public static boolean isImgType(String fileSuffix) {
		for (String s : IMG_FILE_TYPE) {
			if (s.equalsIgnoreCase(fileSuffix)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 根据文件后缀确定 Content-Type
	 */
	public static MediaType determineContentType(String fileSuffix) {
		fileSuffix = fileSuffix.toLowerCase();

		if (isImgType(fileSuffix)) {
            return switch (fileSuffix) {
                case "jpg", "jpeg" -> MediaType.IMAGE_JPEG;
                case "png" -> MediaType.IMAGE_PNG;
                case "gif" -> MediaType.IMAGE_GIF;
                default -> MediaType.APPLICATION_OCTET_STREAM;
            };
		} else if (bVideoFileType("example." + fileSuffix)) {
            return switch (fileSuffix) {
                case "mp4" -> MediaType.valueOf("video/mp4");
                case "avi" -> MediaType.valueOf("video/x-msvideo");
                case "wmv" -> MediaType.valueOf("video/x-ms-wmv");
                default -> MediaType.APPLICATION_OCTET_STREAM;
            };
		} else if (bAudioFileType("example." + fileSuffix)) {
            return switch (fileSuffix) {
                case "mp3" -> MediaType.valueOf("audio/mpeg");
                case "wav" -> MediaType.valueOf("audio/wav");
                case "flac" -> MediaType.valueOf("audio/flac");
                case "aac" -> MediaType.valueOf("audio/aac");
                case "ogg" -> MediaType.valueOf("audio/ogg");
                case "m4a" -> MediaType.valueOf("audio/mp4"); // 常用于M4A
                case "ape" -> MediaType.valueOf("audio/ape"); // 部分播放器识别
                default -> MediaType.APPLICATION_OCTET_STREAM;
            };
		} else if (bSubtitleFileType("example." + fileSuffix)) {
            return switch (fileSuffix) {
                case "srt" -> MediaType.valueOf("application/x-subrip");
                case "ass" -> MediaType.valueOf("text/x-ssa");
                case "vtt" -> MediaType.valueOf("text/vtt");
                case "lrc" -> MediaType.TEXT_PLAIN;
                default -> MediaType.APPLICATION_OCTET_STREAM;
            };
		} else if (bLogFileType("example." + fileSuffix)) {
            return switch (fileSuffix) {
                case "txt", "log" -> MediaType.TEXT_PLAIN;
                case "doc", "docx" -> MediaType.valueOf("application/msword");
                default -> MediaType.APPLICATION_OCTET_STREAM;
            };
		} else if ("pdf".equals(fileSuffix)) {
			return MediaType.APPLICATION_PDF;
		}

		// 默认类型
		return MediaType.APPLICATION_OCTET_STREAM;
	}
}
